import { apiWrapper } from '@/utils/request'

// ========================= 用户端交易API (/User/trade) =========================
export const userTradeApi = {
  // 获取用户交易记录
  getUserTrades: (params) => apiWrapper.get('/User/trade/records', { params }),

  // 用户交易记录查询
  pageTrade: (queryData) => {
    return apiWrapper.post('/user/trade/pageTrade', queryData)
  },

  // 用户转账
  transfer: (transferData) => {
    return apiWrapper.post('/user/trade/transfer', transferData)
  }
}

// ========================= 管理员交易管理API (/Admin/trade) =========================
export const adminTradeApi = {
  // 删除交易记录
  deleteTrade: (tradeid) => {
    return apiWrapper.delete(`/Admin/trade/deleteTrade/${tradeid}`)
  },

  // 分页查询交易记录
  pageTrade: (queryData, begin, end) => {
    // 构建正确的请求体，匹配TradeQueryDTO，避免null值导致后端NPE
    const requestBody = {
      tradeid: queryData.tradeid || 0,
      cardid: queryData.cardid || '',
      tradetype: queryData.tradetype || '',
      minMoney: queryData.minAmount || 0,
      maxMoney: queryData.maxAmount || 0,
      remark: queryData.remark || '',
      page: queryData.page || 1,
      pageSize: queryData.pageSize || 20
    }

    // 构建查询参数
    const params = new URLSearchParams()
    if (begin) params.append('begin', begin)
    if (end) params.append('end', end)

    const url = `/Admin/trade/pageTrade${params.toString() ? '?' + params.toString() : ''}`
    
    return apiWrapper.post(url, requestBody)
  },
}

// 兼容旧版本的导出
export const getTrades = adminTradeApi.pageTrade
export const deleteTrade = adminTradeApi.deleteTrade
export const getUserTrades = userTradeApi.getUserTrades 